package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbClazzLevel;
import com.example.ymm.modules1.mapper.my.user.DbClazzLevelMapper;
import com.example.ymm.modules1.service.dto.DbClazzLevelQueryCriteria;
import com.example.ymm.modules1.service.user.DbClazzLevelService;
import com.example.ymm.util.QueryHelpPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class DbClazzLevelServiceImpl extends BaseServiceImpl<DbClazzLevelMapper, DbClazzLevel> implements DbClazzLevelService {

    @Override
    public TableList<DbClazzLevel> list(DbClazzLevelQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbClazzLevel> page = new Page<DbClazzLevel>(pageDto.getPage(),pageDto.getPageSize());
        baseMapper.selectPage(page, QueryHelpPlus.getPredicate(DbClazzLevel.class, criteria));
        TableList<DbClazzLevel> tableList=new TableList(page);
        return tableList;
    }

    public List<DbClazzLevel> queryAll(DbClazzLevelQueryCriteria criteria) {
        List<DbClazzLevel> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbClazzLevel.class, criteria));
        return result;
    }

    @Override
    public void setLevels(String clazzId, String levels) {
        LambdaQueryWrapper<DbClazzLevel> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DbClazzLevel::getClazzId,clazzId);
        remove(queryWrapper);
        String[] split = levels.split(",");
        List<DbClazzLevel> list=new ArrayList<>();
        for(String l:split){
            DbClazzLevel level=new DbClazzLevel();
            level.setClazzId(clazzId);
            level.setLevelName(l);
            list.add(level);
        }
        saveOrUpdateBatch(list);
    }
}
